// pagesMer/pages/orderCoupon/reservation/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"),
common_Js = require("../../../../component/common.js");
import Toast from '../../../../component/vant/toast/toast';
import Dialog from '../../../../component/vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coupon_id: 0,
    phone_num:'',
    real_name:'',
    user_address:'',
    address:{},
    coupon:{},
    use_time:'',
    use_count: 1,
    remark:'',
    isShowPopup: false,
    minHour: 10,
    maxHour: 20,
    minDate: new Date().getTime(),
    maxDate: new Date(2099, 12, 31).getTime(),
    currentDate: new Date().getTime(),
    bucket_count:0,
    isShowError: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.coupon_id = parseInt(options.id)
    }
    this.data.phone_num = app.getStorageSync('phone_num');
    this.setData({
      phone_num: this.data.phone_num,
    })
    var today = new Date(); // 获取当前日期
    var tomorrow = new Date(today);
    tomorrow.setDate(today.getDate() + 1); // 将当前日期加1天

    this.data.use_time = common_Js.formatTimeSimple(tomorrow);
    console.log('use_time', this.data.use_time)
    this.setData({
      minDate: tomorrow.getTime(),
      use_time: this.data.use_time,
    });

    this.onHttpInit();
  },
  onShow(){
    console.log('address', this.data.address);
    if(this.data.address!=undefined){
      this.setData({
        address:  this.data.address,
        user_address: this.data.address.address,
        phone_num: this.data.address.phoneNum,
        real_name: this.data.address.receiverName,
      })
    }
  },
  inputHandle(e){
    console.log('inputHandle', e)
    let key = e.currentTarget.dataset.key;
    if(key == 'name'){
      this.data.real_name = e.detail;
    } else if(key == 'phone'){
      this.data.phone_num = e.detail;
    } else if(key == 'remark'){
      this.data.remark = e.detail;
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/product/order/coupon/detail', {
      coupon_id: that.data.coupon_id,
    }, function (res) {
      if (res.data.code == 0) {
        that.data.coupon = res.data.data.coupon;
        that.data.address =  res.data.data.address;
        if(res.data.data.address!=undefined){
          that.data.user_address = res.data.data.address.address;
          that.data.real_name = res.data.data.address.receiverName;
        }
        let coupon = res.data.data.coupon
        if(coupon.isLimitBucket == 1 && res.data.data.bucket_count == 0){
          that.setData({
            isShowError: true,
          })
        }
        that.setData({
          bucket_count:  res.data.data.bucket_count,
          coupon: res.data.data.coupon,
          user_address: that.data.user_address,
          real_name: that.data.real_name
        })
      }
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  }, 
  chooseAddress(){
    wx.navigateTo({
      url: "/pages/address/index?orderType=7"
    })
  },
  showPop(){
    this.setData({
      isShowPopup: !this.data.isShowPopup
    })
  },
  onChangeCount(e){
    console.log('onChangeEggCount',e);
    this.data.use_count = e.detail;
    //如果是预约的大桶水，要判断已经付押金的桶数
    // 
    if(this.data.coupon.isLimitBucket == 1 && this.data.use_count > this.data.bucket_count){
      this.setData({
        isShowError: true
      })
    }else{
      this.setData({
        isShowError: false
      })
    }
    this.setData({
      use_count:e.detail
    })
  },
  goPayBucket(){
    wx.navigateTo({
      url: '/pages/bucket/buy/index',
    })
  },
  onTimeInput(e) {
    console.log('onTimeInput', e)
    this.data.use_time = common_Js.formatTimeSimple(new Date(e.detail));
    this.setData({
      use_time: this.data.use_time,
      currentDate: e.detail,
    });
    console.log('use_time', this.data.use_time)
    this.showPop();
  },
  
  /**
   * 获取页面数据的 http请求
   */
  onHttpSubmit: function () {
    var that = this;
    if(this.data.use_count <= 0){
      Toast('请选择预约份数');return;
    }
    if(this.data.phone_num == ''){
      Toast('请输入联系方式');return;
    }
    if(this.data.real_name == ''){
      Toast('请输入收货人');return;
    }
    if(this.data.user_address == ''){
      Toast('请选择收货地址');return;
    }
    if(this.data.use_time == ''){
      Toast('请选择预约时间');return;
    }
    
    if(this.data.coupon.isLimitBucket == 1 && this.data.use_count > this.data.bucket_count){
      Toast('预约数量超过已付押金的桶数');return;
    }

    fetchApi.httpPost('/product/order/coupon/reservation', {
      coupon_id: that.data.coupon_id,
      phone_num: that.data.phone_num,
      address: that.data.user_address,
      real_name: that.data.real_name,
      use_count: that.data.use_count,
      use_time: that.data.use_time + ' 00:00:00',
      remark: that.data.remark,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1
          })
        }, 300)
      }else{
        Toast.fail(res.data.msg);
      }
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  }, 
})